import { Component,ChangeDetectorRef } from '@angular/core';
import { NavController,NavParams,LoadingController  } from 'ionic-angular';
import { APP_YUANCHNEG_URL } from '../../providers/Constants';
import {HttpService} from '../../providers/HttpService';
import { NativeServicetwo } from "../../providers/NativeServicetwo";


 declare let cordova: any;
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  statee: string = './assets/imgs/start.png';
  Stop: string = './assets/imgs/stop.png';
  str:string;
   wrist:any="";//腕带
   oTube:any="";//试管
   token:any;
   S1="";
   Medication=false;//服药div
   drawblood=false;//采血div
  all_reset=false;//重置按钮
  drawblood1=true;
   drawblood2=false;
   drawbloodshenfen=false;//身份认证按钮
   thatId="";
   subjectsId="";
   all_one=true;
   all_txt=false;
   SUBid="";
   sex="";
   age="";
   processName="";
   projectName="";
    resultt="";
    Resultt="";
    avatarPath1='';//图片1
    avatarPath2='';//图片2
    imgAll=false;
    oneren=false;
    tworen=false;
    path1:"";
    path2:"";
    MSG="";//识别信息
    img22=false;
    //
    drawbloodshenfen2=false;
    tworen2=false;
    oneren2=false;
drawblood3=false;
tworen3=false;
oneren3=false;
projectCyc='';

  constructor(public navCtrl: NavController,
                  private httpService: HttpService,public navParams: NavParams,public cd: ChangeDetectorRef,
                  private nativeService: NativeServicetwo,
                  public loadingCtrl: LoadingController
                  ) {
                  this.token=this.navParams.data.value.login_token;
  }
    all(){
      cordova.plugins.test.star(result=>{
                this.str=result;
                this.S1=this.str.substr(0, 1);

                    let obj={
                        code:this.str,
                        login_token:this.token
                    }
                  
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/barCode',obj).then(res=>{
                       
                       if(res.code=="111"||res.code=="222"){
                            if(res.value.state == "0" ){
                              //服药
                              
                               this.subjectsId=res.value.subjectsId;
                               this.thatId=res.value.id;
                              this.all_one=false;
                              this.Medication=true;
                              this.all_reset=true;//重置按钮
                              this.all_txt=true;
                               this.SUBid=res.value.name;
                               this.sex=res.value.sex;
                               this.age=res.value.randomNum;
                               this.processName=res.value.processName;
                               this.projectName=res.value.pnum;
                              this.resultt=this.str;
                              this.projectCyc=res.value.cycles;
                              
                            }else if(res.value.state == "1" && this.S1 == "S"){
                              //采血
                              this.all_one=false;
                              this.drawblood=true;
                              this.wrist=res.value.subjectsId;
                              this.all_txt=true;
                              this.all_reset=true;//重置按钮
                             this.SUBid=res.value.name;
                             this.sex=res.value.sex;
                             this.age=res.value.randomNum;
                             this.processName=res.value.processName;
                             this.projectName=res.value.pnum;
                             this.projectCyc=res.value.cycles;
                               this.thatId=res.value.id;
                              if(res.code=="222"){
                                  alert(res.msg)
                              }


                            }else{
                              alert("请正确扫描");
                            }
                           
                              
                           
                            this.cd.detectChanges();
                       }else if(res.code=="001"){
                         alert(res.msg);
                            this.all_one=false;
                              this.all_reset=false;//重置按钮
                             this.SUBid=res.value.name;
                             this.sex=res.value.sex;
                             this.age=res.value.random_num;
                             this.processName=res.value.process_name;
                             this.projectName=res.value.pnum;
                               this.thatId=res.value.id;
                               this.subjectsId=res.value.subjects_id;
                            this.all_txt=true;
                            this.projectCyc=res.value.cycles;
                            this.drawblood3=true;

                               this.cd.detectChanges();
                       }else{
                          alert(res.msg);
                       }
                        
                     })
                
            },error=>console.log(error));
    }
          //服药阶段
          Medication1_c(){
            
            
               
                    let obj={
                        id:this.thatId,
                        subjectsId:this.subjectsId,
                        login_token:this.token
                    }
                  
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/subjectsProcess/update',obj).then(res=>{
                        if(res.code="111"){
                            this.all_reset=false;
                              this.all_txt=false;
                              this.Medication=false;
                              this.drawbloodshenfen2=true;
                            this.cd.detectChanges(); 

                        }else{
                             this.all_reset=false;//重置按钮
                              this.all_txt=false;
                                   this.SUBid="";
                                   this.sex="";
                                   this.age="";
                                   this.processName="";
                                   this.projectCyc="";
                                   this.projectName="";
                                   this.Medication=false;
                                    this.all_one=true;
                                    this.Resultt="";
                                    this.resultt="";
                            this.cd.detectChanges(); 
                        }

                      
                       alert(res.msg);
                        
                     })

               
                 
           
            
                
                

               
            }
            drawblood1_c(){
                cordova.plugins.test.star(result=>{
                    this.str=result;
                   this.S1=this.str.substr(0, 1);
                   

                   
                         let obj={
                            id:this.thatId,
                            code:this.str,
                            login_token:this.token
                        }
                        this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/qrBarCodeValide/barCode',obj).then(res=>{
                                
                                if(res.code=="111"||res.code=="222"){ 

                                    this.oTube=res.value.subjectsId;
                                    

                                    if(this.oTube == this.wrist && res.value.state=="1" && this.S1=="C"){
                                        this.resultt=this.str;
                                        this.drawblood1=false;
                                        this.drawblood2=true;
                                        this.all_reset=false;
                                        this.drawbloodshenfen=true;
                                        this.thatId=res.value.id;
                                        this.subjectsId=res.value.subjectsId;
                                        alert("对比成功");
                                        if(res.code=="222"){
                                          alert(res.msg);
                                        }
                                        
                                    }else{
                                        
                                        this.drawblood=false;
                                        this.all_one=true;
                                        this.all_reset=false;//重置按钮
                                        this.oTube="";
                                        this.wrist="";
                                        alert("对比失败");
                                        this.all_txt=false;
                                         this.SUBid="";
                                         this.sex="";
                                         this.age="";
                                         this.processName="";
                                         this.projectCyc="";
                                         this.projectName="";
                                        
                                    }

                                    this.cd.detectChanges(); 
                               }else{

                                    alert(res.msg);
                                    this.drawblood=false;
                                    
                                    this.all_reset=false;//重置按钮
                                    this.all_txt=false;
                                    this.all_one=true;
                                     this.SUBid="";
                                     this.sex="";
                                     this.age="";
                                     this.processName="";
                                     this.projectCyc="";
                                     this.projectName="";
                                     this.cd.detectChanges(); 
                               }
                                
                               
                                
                             })
                   
                   
                },error=>console.log(error));
                    

                
            }
            drawblood2_c(){
               
                     
                          let obj={
                            id:this.thatId,
                            subjectsId:this.subjectsId,
                            login_token:this.token
                        }
                        this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/subjectsProcess/update',obj).then(res=>{

                              
                               
                               alert(res.msg);
                                
                             })
                      
                        this.all_reset=false;//重置按钮
                         this.all_txt=false;
                               this.SUBid="";
                               this.sex="";
                               this.age="";
                               this.processName="";
                               this.projectCyc="";
                               this.projectName="";
                               this.drawblood2=false;
                               this.drawbloodshenfen=false;
                               this.drawblood1=true;
                               this.drawblood=false;
                               this.all_one=true;
                                this.oTube="";
                                this.wrist="";
                                this.str="";
                    this.cd.detectChanges(); 
                   
              
            }
             drawblood3_c(){
               
                     
                          let obj={
                            id:this.thatId,
                            subjectsId:this.subjectsId,
                            login_token:this.token
                        }
                        this.httpService.post(APP_YUANCHNEG_URL+'/api/pt/subjectsProcess/update02',obj).then(res=>{

                         
                               
                               alert(res.msg);
                                
                             })
                      this.drawblood3=false;
                        this.all_reset=false;//重置按钮
                         this.all_txt=false;
                               this.SUBid="";
                               this.sex="";
                               this.age="";
                               this.processName="";
                               this.projectCyc="";
                               this.projectName="";
                               this.drawblood2=false;
                               this.drawbloodshenfen=false;
                               this.drawblood1=true;
                               this.drawblood=false;
                               this.all_one=true;
                               this.drawblood3=false;
                                this.oTube="";
                                this.wrist="";
                                this.str="";
                    this.cd.detectChanges(); 
                   
              
            }
            getPictureGO1(){
              this.all_txt=false;
              this.imgAll=true;
              this.drawblood=false;
              this.all_reset=false;
              this.img22=false;
              this.oneren=true;
              this.cd.detectChanges(); 


            }
            //重采
             getPictureGO11(){
              this.all_txt=false;
              this.imgAll=true;
              this.all_reset=false;
              this.img22=false;
              this.oneren3=true;
              this.drawblood3=false;
              this.cd.detectChanges(); 


            }
             getPictureGO21(){
              this.all_txt=false;
              this.imgAll=true;
              this.all_reset=false;
              this.img22=true;
              this.tworen3=true;
              this.drawblood3=false;
              
              this.cd.detectChanges(); 


            }
            getPictureGO2(){
              this.all_txt=false;
              this.imgAll=true;
              this.drawblood=false;
              this.all_reset=false;
              this.tworen=true;
              this.img22=true;
                    this.cd.detectChanges(); 


            }
            getPictureGO1s(){
                this.img22=false;
                 this.drawbloodshenfen2=false;
                    this.imgAll=true;
                    this.oneren2=true;
                    this.cd.detectChanges(); 
            }
            getPictureGO2s(){
                this.drawbloodshenfen2=false;
                    this.imgAll=true;
                    this.tworen2=true;
                     this.img22=true;
                    this.cd.detectChanges(); 
            }
            //人脸识别按钮
            getPicture1(){
              let options = {
                 
                };
              this.nativeService.getPictureByCamera(options).then(imageBase64 => {
                  this.getPictureSuccess(imageBase64);
                  
                });
            }
             //人脸识别按钮
            getPicture2(){
              let options = {
                    
                };
              this.nativeService.getPictureByCamera(options).then(imageBase64 => {
                  this.getPictureSuccess2(imageBase64);
                  
                });
            }
            private getPictureSuccess(imageBase64) {
               
                  this.avatarPath1 = 'data:image/jpeg;base64,'+imageBase64;
                  
               
            }
            private getPictureSuccess2(imageBase64) {
               if(this.avatarPath1==''){
                  this.avatarPath1 = 'data:image/jpeg;base64,'+imageBase64;
                 
               }else{
                  this.avatarPath2 = 'data:image/jpeg;base64,'+imageBase64;
                  
                  

               }
            }
            getPicture22(){
               
            }
            //重采单人提交
            getPictureSub1_cc(){
                let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });

               
                loading.present();
                let obj={
                      face1:this.avatarPath1,
                      type:1,
                      id:this.thatId,
                       subjectsId:this.subjectsId,
                        login_token:this.token
                    }
                  
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allface',obj).then(res=>{
                      

                      if(res.code=="111"){
                          alert(res.msg);
                          this.imgAll=false;
                          this.oneren3=false;
                          this.all_txt=true;
                          
                          this.drawblood3=true;
                            this.avatarPath1 = '';
                         this.MSG="";
                         this.drawbloodshenfen2=false;
                          



                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 

                        
                     })
            }
             //双人重采提交认证
            getPictureSub2_cc(){
                  let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });
                loading.present();
                  var obj={
                      face1:this.avatarPath1,
                      face2:this.avatarPath2,
                      type:"2",
                      id:this.thatId,
                       subjectsId:this.subjectsId,
                        login_token:this.token
                    }
                    
                    
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allface',obj).then(res=>{
                      
                        if(res.code=="111"){
                            

                            this.imgAll=false;
                            this.tworen3=false;
                            this.all_txt=true;
                            this.img22=false;
                            this.drawblood3=true;
                            this.avatarPath1 = '';
                            this.avatarPath2 = '';
                            this.MSG="";
                            alert(res.msg);
                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }else if(res.value=='2'){
                            this.avatarPath2="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 
                       
                        
                     })
            }
            //单人提交认证
            getPictureSub1(){
                let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });

               
                loading.present();
                let obj={
                      face1:this.avatarPath1,
                      type:1,
                      id:this.thatId,
                       subjectsId:this.subjectsId,
                        login_token:this.token
                    }
                  
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allface',obj).then(res=>{
                      

                      if(res.code=="111"){
                          alert(res.msg);
                          this.imgAll=false;
                          this.oneren=false;
                          this.all_txt=true;
                          
                          this.drawblood=true;
                            this.avatarPath1 = '';
                         this.MSG="";
                         this.drawbloodshenfen2=false;
                          



                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 

                        
                     })
            }

            getPictureSub_1(){
                let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });

               
                loading.present();
                let obj={
                      face1:this.avatarPath1,
                      type:1,
                      id:this.thatId,
                       subjectsId:this.subjectsId,
                        login_token:this.token
                    }
                  
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allface',obj).then(res=>{
                      

                      if(res.code=="111"){
                             this.imgAll=false;
                         this.MSG="";


                         this.drawbloodshenfen2=false;

                          this.all_reset=false;//重置按钮
                          this.str="";
                         this.wrist="";//腕带
                         this.oTube="";//试管
                         this.S1="";
                         this.Medication=false;//服药div
                         this.drawblood=false;//采血div
                        this.all_reset=false;//重置按钮
                        this.drawblood1=true;
                         this.drawblood2=false;
                        this.drawbloodshenfen=false;
                         this.thatId="";
                         this.subjectsId="";
                         this.all_one=true;
                         this.all_txt=false;
                         this.SUBid="";
                         this.sex="";
                         this.age="";
                         this.processName="";
                         this.projectCyc="";
                         this.projectName="";
                          this.resultt="";
                          this.Resultt="";
                          this.oneren=false;
                          this.oneren2=false;
                          this.tworen=false;
                          this.tworen2=false;
                           this.avatarPath1 = '';

                              this.avatarPath2 = '';
                          alert(res.msg);
                          
                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 

                        
                     })
            }

             //双人提交认证
            getPictureSub2(){
                  let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });
                loading.present();
                  var obj={
                      face1:this.avatarPath1,
                      face2:this.avatarPath2,
                      type:"2",
                      id:this.thatId,
                       subjectsId:this.subjectsId,
                        login_token:this.token
                    }
                    
                    
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allface',obj).then(res=>{
                      
                        if(res.code=="111"){
                            

                            this.imgAll=false;
                            this.tworen=false;
                            this.all_txt=true;
                            this.img22=false;
                            this.drawblood=true;
                            this.avatarPath1 = '';
                            this.avatarPath2 = '';
                            this.MSG="";
                            alert(res.msg);
                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }else if(res.value=='2'){
                            this.avatarPath2="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 
                       
                        
                     })
            }
            getPictureSub_2(){
                  let loading = this.loadingCtrl.create({
                  spinner: 'bubbles',
                  content: `
                    <div class="custom-spinner-container">
                      <div class="custom-spinner-box"></div>请稍等……
                    </div>`
                });
                loading.present();
                  var obj={
                      face1:this.avatarPath1,
                      face2:this.avatarPath2,
                      type:"2",
                      id:this.thatId,
                       subjectsId:this.subjectsId,
                        login_token:this.token
                    }
                    
                    
                    this.httpService.post(APP_YUANCHNEG_URL+'/api/lib/face/allface',obj).then(res=>{
                      
                        if(res.code=="111"){
                            



                               this.imgAll=false;
                         this.MSG="";


                         this.drawbloodshenfen2=false;

                          this.all_reset=false;//重置按钮
                          this.str="";
                         this.wrist="";//腕带
                         this.oTube="";//试管
                         this.S1="";
                         this.Medication=false;//服药div
                         this.drawblood=false;//采血div
                        this.all_reset=false;//重置按钮
                        this.drawblood1=true;
                         this.drawblood2=false;
                        this.drawbloodshenfen=false;
                         this.thatId="";
                         this.subjectsId="";
                         this.all_one=true;
                         this.all_txt=false;
                         this.SUBid="";
                         this.sex="";
                         this.age="";
                         this.processName="";
                         this.projectCyc="";
                         this.projectName="";
                          this.resultt="";
                          this.Resultt="";
                          this.oneren=false;
                          this.oneren2=false;
                          this.tworen=false;
                          this.tworen2=false;
                           this.avatarPath1 = '';

                              this.avatarPath2 = '';
                          alert(res.msg);
                            
                        }else{
                          this.MSG=res.msg;
                          if(res.value=='1'){
                              this.avatarPath1="";
                          }else if(res.value=='2'){
                            this.avatarPath2="";
                          }
                        }
                        loading.dismiss();
                        this.cd.detectChanges(); 
                       
                        
                     })
            }



            //放弃认证
             giveup(){
                this.imgAll=false;
                this.tworen=false;
                this.oneren=false;
                this.all_txt=true;
                
                this.drawblood=true;
                this.avatarPath1 = '';
                this.avatarPath2 = '';
                this.MSG='';
              this.img22=false;

                this.cd.detectChanges(); 

             }
               giveup2(){
                this.imgAll=false;
                this.tworen2=false;
                this.oneren2=false;
                this.drawbloodshenfen2=true;
                this.avatarPath1 = '';
                this.avatarPath2 = '';
                this.MSG='';
                this.img22=false;

                this.cd.detectChanges();

             }
             giveup3(){
                this.imgAll=false;
                this.tworen3=false;
                this.oneren3=false;
                this.drawblood3=true;
                this.avatarPath1 = '';
                this.avatarPath2 = '';
                this.MSG='';
                this.img22=false;

                this.cd.detectChanges();

             }
              

           gevidence(){
                this.imgAll=false;
                
              this.drawbloodshenfen2=false;
               this.all_reset=false;//重置按钮
                this.str="";
               this.wrist="";//腕带
               this.oTube="";//试管
               this.S1="";
               this.Medication=false;//服药div
               this.drawblood=false;//采血div
              this.all_reset=false;//重置按钮
              this.drawblood1=true;
               this.drawblood2=false;
              this.drawbloodshenfen=false;
               this.thatId="";
               this.subjectsId="";
               this.all_one=true;
               this.all_txt=false;
               this.SUBid="";
               this.sex="";
               this.age="";
               this.processName="";
               this.projectCyc="";
               this.projectName="";
                this.resultt="";
                this.Resultt="";
                this.oneren=false;
                this.tworen=false;
                 this.avatarPath1 = '';
                 this.avatarPath2 = '';
                    this.cd.detectChanges(); 
           }
            reset(){
              this.drawbloodshenfen2=false;
                          this.imgAll=false;
                

                this.all_reset=false;//重置按钮
                this.str="";
               this.wrist="";//腕带
               this.oTube="";//试管
               this.S1="";
               this.Medication=false;//服药div
               this.drawblood=false;//采血div
              this.all_reset=false;//重置按钮
              this.drawblood1=true;
               this.drawblood2=false;
               this.drawblood3=false;
              this.drawbloodshenfen=false;
               this.thatId="";
               this.subjectsId="";
               this.all_one=true;
               this.all_txt=false;
               this.SUBid="";
               this.sex="";
               this.age="";
               this.processName="";
               this.projectCyc="";
               this.projectName="";
                this.resultt="";
                this.Resultt="";
                this.oneren=false;
                this.oneren2=false;
                this.tworen=false;
                this.tworen2=false;
                 this.avatarPath1 = '';
                 this.drawblood3=false;
                              this.avatarPath2 = '';
                this.img22=false;
                              
                    this.cd.detectChanges(); 
            }

           // stop(){
           // cordova.plugins.test.stop();
           // }
 }
